<?php

namespace EM\NewsBundle\Controller;
use EM\NewsBundle\Entity\Source;
use EM\NewsBundle\Form\SourceType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class SourceController extends Controller
{
   
    public function indexAction(){
                $src = new Source();
		$form = $this->createForm(new SourceType(), $src);
                $repository=$this->getDoctrine()->getRepository('EMNewsBundle:Source');
		$source = $repository->findAll();	
                return $this->render('EMNewsBundle:Source:index.html.twig', array(
                'form' => $form->createView(),
                'status_form'=>0,
                'status'=>0,//object updated
                'source'=>$source
                ));
    }
	public function createAction(Request $request)
    {
	

		$src = new Source();
		$form = $this->createForm(new SourceType(), $src);
		$request = $this->getRequest();
                $repository=$this->getDoctrine()->getRepository('EMNewsBundle:Source');
		$source = $repository->findAll();	
		//-- En caso de que el request haya sido invocado por POST
		//   procesaremos el formulario
		if($request->getMethod() == 'POST')
		{
			//-- Pasamos el request el método bindRequest() del objeto 
			//   formulario el cual obtiene los datos del formulario
			//   y los carga dentro del objeto Article que está contenido
			//   también dentro del objeto Type
			$form->bind($request);

			if($form->isValid())
			{
				$em = $this->getDoctrine()->getEntityManager();
				$em->persist($src);
				$src=new Source();
				$form = $this->createForm(new SourceType(), $src);
				$em->flush();
                                $repository=$this->getDoctrine()->getRepository('EMNewsBundle:Source');
                                $source = $repository->findAll();
				return $this->render('EMNewsBundle:Source:_new.html.twig', array(
				'ex'=>'Object created',
                                'form' => $form->createView(),
                                'status_form'=>1,
				'status'=>1,//object created
                                'source'=>$source
                                ));
			}
			else
			{
			return $this->render('EMNewsBundle:Source:_new.html.twig', array(
			'form' => $form->createView(),
			'status_form'=>2,
                        'source'=>$source
		));
			}
		
		}
			
		return $this->render('EMNewsBundle:Source:_new.html.twig', array(
			'form' => $form->createView(),
			'status_form'=>0,
                        'source'=>$source
		));
    }
 
    
        public function showAction(){

                        $repository=$this->getDoctrine()->getRepository('EMNewsBundle:Source');
                        $source = $repository->findAll();	

                        return $this->render('EMNewsBundle:Source:list.html.twig',array('source' => $source,'status'=>0));

         }
	public function editAction($id)
        {


                    $em = $this->getDoctrine()->getManager();
                    $src=$em->getRepository('EMNewsBundle:Source')->find($id);
                    $form = $this->createForm(new SourceType(), $src);
                    $request = $this->getRequest();
                    //-- En caso de que el request haya sido invocado por POST
                    //   procesaremos el formulario
                    if($request->getMethod() == 'POST')
                    {
                            //-- Pasamos el request el método bindRequest() del objeto 
                            //   formulario el cual obtiene los datos del formulario
                            //   y los carga dentro del objeto Article que está contenido
                            //   también dentro del objeto Type
                            $form->bind($request);

                            //-- Con esto nuestro formulario ya es capaz de decirnos si
                            //   los dato son válidos o no y en caso de ser así
                            if($form->isValid())
                            {

                                    $em->persist($src);
                                    $em->flush();
                                    $src=new Source();
                                    $form = $this->createForm(new SourceType(), $src);
                                    $repository=$this->getDoctrine()->getRepository('EMNewsBundle:Source');
                                    $source = $repository->findAll();
                                    //return $this->redirect($this->generateUrl('em_src_list'));
                                    return $this->render('EMNewsBundle:Source:_new.html.twig', array(
				'ex'=>'Object updated',
                                'form' => $form->createView(),
                                'status_form'=>3,
				'status'=>2,//object updated
                                'source'=>$source
                                ));
                            }
                            else
                            {
                            return $this->render('EMNewsBundle:Source:edit.html.twig', array(
                            'form' => $form->createView(),
                            'error'=>1,
                    ));
                            }
                    }


                    return $this->render('EMNewsBundle:Source:edit.html.twig', array(
                            'form' => $form->createView(),
                            'id'=>$id,
                            'error'=>0			
                    ));
        }
        public function findAction($id){
            $em = $this->getDoctrine()->getManager();
            $src=$em->getRepository('EMNewsBundle:Source')->find($id);
            $form = $this->createForm(new SourceType(), $src);
            return $this->render('EMNewsBundle:Source:edit.html.twig', array(
                            'form' => $form->createView(),
                            'id'=>$id,
                            'error'=>0			
                    ));
        }
	public function deleteAction($id)
        {
            $exception=null;
            $status=0;
		$em = $this->getDoctrine()->getManager();
		$src=$em->getRepository('EMNewsBundle:Source')->find($id);                
                try{
                     $em->remove($src);
                     $em->flush();
                     
                } catch ( \Doctrine\DBAL\DBALException $e)
                {   
                   //  return $this->render('EMNewsBundle:Source:list.html.twig',array('source' => $source,'ex'=>$e));
                    $exception=$e->getMessage();
                    $status=3;
                }
                     $repository=$em->getRepository('EMNewsBundle:Source');
                     $source = $repository->findAll();
                     return $this->render('EMNewsBundle:Source:list.html.twig',array('source' => $source,'ex'=>$exception,'status'=>$status));
            
		
		
         }
	
	
}
